/**
 * pda红外线扫码
 */
var main = ''
var filter = ''
var receiver = ''
var codeQueryTag = false
let scanSuccess = function() {} // 成功后的操作函数
//开启服务监听
export function initScan(successFun, type) {
	scanSuccess = successFun
	// let action=type?"com.rscja.scanner.action.scanner.RFID":"com.scanner.broadcast"
	let action = type == "RFID" ? "com.rscja.scanner.action.scanner.RFID" : "com.scanner.broadcast"
	console.log(`开启了监听${type?"RFID":"红外"}`);
	main = plus.android.runtimeMainActivity(); //获取activity  
	console.log(main)
	var IntentFilter = plus.android.importClass('android.content.IntentFilter');
	filter = new IntentFilter();
	filter.addAction(action); // 换你的广播动作  (设置-扫描设置-输出方式-输出方式改为Intent输出) 
	receiver = plus.android.implements('io.dcloud.android.content.BroadcastReceiver', {
		onReceive: function(context, intent) {
			plus.android.importClass(intent);
			let code = intent.getStringExtra("data") || intent.getStringExtra("com.symbol.datawedge.data_string"); // 换你的广播标签 
			queryCurrenCode(code);
		}
	});
	console.log('开启监听完成');
}
//这个得有，开启监听用
export function startScan() {
	main.registerReceiver(receiver, filter);
}
//这个也得有，关闭监听用
export function stopScan() {
	main.unregisterReceiver(receiver);
}
//这个是防止重复用的
async function queryCurrenCode(code) {
	//防重复  
	if (codeQueryTag) return false;
	codeQueryTag = true;
	setTimeout(() => {
		codeQueryTag = false;
	}, 150);
	console.log('调用接口啦------', code);
	scanSuccess(code)
}